﻿using System;
using System.Windows.Forms;
using System.Data;
using System.Data.Sql;

using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;

using prjKQXS_UI.View;
using prjKQXS_DLL;

namespace prjKQXS_UI.Security
{
    public partial class frmConfigDatabase : XtraForm
    {
        private string path = BienTc.fileIni;
        string callerFormName = "";

        #region form's event =====================================================
        public frmConfigDatabase(string callerFormName)
        {
            InitializeComponent();
            this.callerFormName = callerFormName;
        }

        private void frmConfigDatabase_Shown(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            cbbChungThuc.SelectedIndex = 0;
            cbbChungThuc.Enabled = false;

        }
        #endregion ===============================================================


        #region button's event ===================================================
        private void sbtnGetServer_Click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(frmWaitForm));
            {
                cbbServerName.Items.Clear();
                cbbServerName.Enabled = false;

                // Retrieve the enumerator instance and then the data.
                SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
                System.Data.DataTable table = instance.GetDataSources();

                // Display the contents of the table.
                string ServerName = "";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ServerName = table.Rows[i]["ServerName"].ToString().Trim();
                    if (!table.Rows[i]["InstanceName"].ToString().Trim().Equals(""))
                    {
                        if (i == 0) cbbServerName.Items.Add(ServerName);
                        else
                        {
                            if (ServerName == table.Rows[i - 1]["ServerName"].ToString().Trim())
                                cbbServerName.Items.Add(table.Rows[i]["ServerName"] + @"\" + table.Rows[i]["InstanceName"]);
                            else
                            {
                                cbbServerName.Items.Add(table.Rows[i]["ServerName"].ToString());
                                cbbServerName.Items.Add(table.Rows[i]["ServerName"] + @"\" + table.Rows[i]["InstanceName"]);
                            }
                        }
                    }
                    else
                    {
                        cbbServerName.Items.Add(ServerName);
                    }
                }
                cbbServerName.Enabled = true;
            }
            SplashScreenManager.CloseForm();
        }

        private void sbtnKetnoi_Click(object sender, EventArgs e)
        {
            BienTc.connString = "Data Source=" + cbbServerName.Text + "; Initial Catalog = master; User Id=" + teUser.Text + "; Password=" + tePass.Text + ";";
            if (Config.testConnection(BienTc.connString))
            {
                Config.writeConfig(BienTc.connString);

                frmWaiting fWaiting = new frmWaiting();
                frmLogin fLogin = new frmLogin();
                if (callerFormName == fWaiting.Name)fLogin.Show();

                this.Close();
            }
            else clsCommon.MsgBox("Kết nối không thành công, vui lòng thiết lập và thử lại", 1);
        }

        private void sbtnHuy_Click(object sender, EventArgs e)
        {
            frmWaiting fWaiting = new frmWaiting();
            frmLogin fLogin = new frmLogin();

            if (callerFormName == fWaiting.Name) Application.Exit();
            else if (callerFormName == fLogin.Name) this.Close();
        }
        #endregion ===============================================================
    }
}